var circleSize = 280;// 圆盘的大小(直径)
var circleR = circleSize / 2;// 圆盘的半径
var distance = circleR;// 圆心相对于容器的距离（原点）
var fnR = 30;// 小圆的半径
var fnMargin = 55; // 小圆圆心到大圆圆心的距离
var centerR = 40;// 中心圆的半径
var imgWidth = 55;
var imgHeight = 60;
var boxId = "dial-box";// 画布容器html元素ID
var $box = $("#" + boxId);
$box.css({
	width : circleSize,
	height : circleSize
});

var fnArray = [ {
	text : "我要充值",
	src : "images/dial/dangmianfukuan.png",
	click : function() {
		openUrl(BASE_PATH + "kdpay/pay_select.jsp?scene=01");
	}
}, {
	text : "我要收款",
	src : "images/dial/yuanchenshoukuan.png",
	click : function() {
		openUrl(BASE_PATH + "kdpay/pay_select.jsp?scene=02");
	}
}, {
	text : "交易中心",
	src : "images/dial/jiaoyimingxi.png",
	click : function() {
		openUrl(BASE_PATH + "user/user_account.jsp");
	}
},
 {
 text : "我的收入",
 src : "images/dial/wodeshouru.png",
 click:function(){
  openUrl(BASE_PATH + "user/my_income.jsp");
 }
 },
{
	text : "便民服务",
	src : "images/dial/bianmingfuwu.png",
	click : function() {
		openUrl(BASE_PATH + "user/user_service.jsp");
	}
}, {
	text : "大智梦收款",
	src : "images/dial/gongsishoukuan.png",
	click : function() {
		openUrl(BASE_PATH + "kdpay/pay_select.jsp?scene=03");
	}
} ];

var line = {
	stroke : "#fff",
	"stroke-width" : 2,
	"stroke-linecap" : "round",
	"stroke-linejoin" : "round"
}, dots = {
	stroke : "#fff",
	"stroke-width" : 2,
	"stroke-dasharray" : "- ",
	"stroke-linecap" : "round",
	"stroke-linejoin" : "round"
}, fill = {
	stroke : "#fff",
	fill : "#fff",
	"fill-opacity" : .5,
	"stroke-width" : 2,
	"stroke-linecap" : "round",
	"stroke-linejoin" : "round"
}, none = {
	fill : "#000",
	opacity : 0
};

var paper = Raphael(boxId, circleSize, circleSize);
var c = paper.circle(circleR, circleR, circleR).attr({
	fill : "white",
	opacity : "0.2"
});

var c_2 = paper.circle(circleR, circleR, circleR - 10).attr({
	fill : "white",
	opacity : "0.1"
}).mouseover(function(evt) {// 浏览器端移动
// evt.preventDefault();
// for (var i = 0; i < menuList.length; i++) {
// var menu = menuList[i];
// menu.animate({
// "path":
// "M10,10C23.333333333333332,16.666666666666668,90,36.666666666666664,90,50C90,63.333333333333336,23.333333333333332,83.33333333333333,10,90"
// }, 600,"");
// }
}).drag(function(dx, dy) {// 拖动
// $("#test").html("dx:" + dx + ",dy:" + dy);
}, function() {
}).click(function() {
	// menuArray[1]
});

function cancelDefaultEvt(obj) {
	obj.touchmove(function(evt) {
		evt.preventDefault();
	});
}
cancelDefaultEvt(c);
cancelDefaultEvt(c_2);

function prepare(id) {
	var div = document.getElementById(id);
	return Raphael(div, 99, 99);
}

function getAngle(x1, y1, x2, y2) {
	// 直角的边长
	var x = Math.abs(x1 - x2);
	var y = Math.abs(y1 - y2);
	// 斜边长
	var z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
	// 余弦
	var cos = y / z;
	// 弧度
	var radina = Math.acos(cos);
	// 角度
	var angle = 180 / (Math.PI / radina);
	return angle;
}

function getMenuArray() {
	var menuArray = [];
	var len = fnArray.length;
	var model = len % 2;

	for (var i = 0; i < len; i++) {
		var obj = fnArray[i];
		var ratio = 360 / len * i;
		var hudu = (2 * Math.PI / 360) * ratio;

		var X = distance + Math.sin(hudu) * (circleR - fnMargin);

		var Y = distance - Math.cos(hudu) * (circleR - fnMargin);

		var img = paper.image(obj['src'], circleR - imgWidth / 2,
				circleR - imgHeight / 2, imgWidth, imgHeight).data(obj).data(
				"customTurn",
				function(turnParam) {
					var angle = turnParam.angle;
					var time = turnParam.time;
					var direction = turnParam.direction;
					var _this = turnParam.turnIs;
					var callBack = turnParam.callBack || function() {

					};
					(function(r) {
						_this.animate({
							transform : "r" + [ angle, circleR, circleR ] + "r"
									+ [ -angle ]
						}, time, callBack);
					})(paper);
				}).drag(function(dx, dy) {
			// this.data("drag",true);
			// this.animate({
			// x: X - imgWidth/2,
			// y: Y-imgHeight/2
			// }, 600,function(){
			// var _this = this;
			// _this.data("customAnimate")(this,X - imgWidth/2,Y-imgHeight/2);
			// });
			// this.data("customAnimate")(this,dx,dy);
		}, function() {
			// this.data("drag",false);
			// this.transform("s" + [1.1]);
		}, function() {
			// var drag = this.data("drag");
			// this.transform("s" + [1]);
			// 因为某些设备拖动事件会影响单击事件（如苹果设备），所以如果没有实际拖动，则结束后触发单击事件
			// if(!(drag == true)){
			this.data()['click']();
			// }
		}).animate({
			x : X - imgWidth / 2,
			y : Y - imgHeight / 2
		}, 600, function() {

		});

		menuArray[i] = img;
	}
	return menuArray;
}

var menuList = getMenuArray();

var logoWidth = 80;
var logoHeight = 80;
var jiaodu = 0;
paper.image("images/dial/logo.png", circleR - logoWidth / 2,
		circleR - logoHeight / 2, logoWidth, logoWidth).click(function() {
	jiaodu = 30;
	
	_dial.eachTurn(function(obj) {
		_dial.turn({
			angle : jiaodu,
			time : 600,
			turnIs : obj,
			callBack : function(){
//				_dial.getTransform(obj);
			}
		});
	});
//	setTimeout(function() {
//		eachImage(function(obj){
//			obj.stop();
////			alert(obj.attr("transform"));
//		});
//	}, 100);

});

var _dial = {
	turnList : menuList,//转动列表
	turnLength : function(){//获取转动元素列表集合
		return this.turnList ? this.turnList.length : 0;
	},
	turn : function(turnParam){//转动
		var angle = turnParam.angle;
		var time = turnParam.time;
		var _this = turnParam.turnIs;
		var direction = turnParam.direction || 1;//转动方向 1 顺时钟，-1或其他逆时针
		var fixed = turnParam.fixed || 0;//是否是固定的位置,如果是固定角度，不会根据当前的位置计算
		var callBack = turnParam.callBack || function() {};
		var rx = turnParam.rx || circleR;//转动的圆心 x 坐标，默认为原点
		var ry = turnParam.ry || circleR;
		//反方向
		if(direction != 1){
			angle = angle * -1;
		}
		
		var transform = this.getTurnTransform(turnParam);
		
		_this.animate({
			transform : transform
		}, time, callBack);
	},
	eachTurn : function(call){//遍历转动元素，并且执行传入的函数
		var len = this.turnLength();
		if(len == 0){
			return;
		}
		for (var i = 0; i <= len; i++) {
			var obj = this.turnList[i];
			if (obj) {
				if (call) {
					if (typeof call === 'function') {
						call(obj);
					}
				}
			}
		}
	},
	getTransform : function(elm){//获取现在位置已经转动的角度参数
		var t = elm.transform();
		return t;
	},
	getTurnTransform : function(turnParam){
		var _this = turnParam.turnIs;
		var angle = turnParam.angle;

		var direction = turnParam.direction || 1;//转动方向 1 顺时钟，-1或其他逆时针
		var fixed = turnParam.fixed || 0;//是否是固定的位置,如果是固定角度，不会根据当前的位置计算
		
		//反方向
		if(direction != 1){
			angle = angle * -1;
		}
		var transform;
//		alert(this.getTransform(_this).length);
		if(fixed == 1 || this.getTransform(_this).length == 0){
			transform = "r" + [ angle, circleR, circleR ] + "r" + [ -angle ];
			return transform;
		}
		var t = this.getTransform(_this);
//		alert(t);
		return t;
		
	}
}

$(document).ready(function() {
	_dial.eachTurn(function(obj) {
		_dial.turn({
			angle : 360,
			time : 600,
			direction : 1,
			turnIs : obj,
			callBack : function(){
//				_dial.getTransform(obj);
			}
		});
	});
});
